package com.xiaoran.commonapi.tencent;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Random;

public class Alipay {
    private static Alipay instance = new Alipay();

    private Alipay() {
    }

    public static Alipay getInstance() {
        Factory.setOptions(getOptions());
        return instance;
    }

    public String wap_pay() {
        System.out.println("电泳");
        String subject = "吃了么订单支付";
        String totalMny = "15.8";
        String quitUrl = "https://www.lzxr.vip";
        String returnUrl = "https://www.lzxr.vip";
        String outTradeNo = genOutTradeNo();
        try {
            AlipayTradeWapPayResponse rsp = Payment.Wap().pay(subject, outTradeNo, totalMny, quitUrl, returnUrl);
            return rsp.body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "2021000117616890";
        config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIqhzF1kulsYbm4uMj2bC2MfwFIFWiBip1Mq5jYs6qLNY16+gmkf7VuntmZ+2AwshnXzmwP1+J4nRfVbUL1KQa8CN38wb/1wb7aI42YQSrBWeR/2pc49L0xTse1mcpcQWkJ7/fSknR/Pyu1KLFW1Qsye0VRenprrf/iV2q/cFFMX3ZCKIEO7DW1utFHtLTlD3MpNTZQjTP4Chv7x3HMBPOMlHcD9Q5HDxnNAGFz2OXdgwG1fhXMiFtIcX5IvuavUSy3+IpZ0Skhi4nsVXARtjuuGOqOeWp/qjDXEqCjAsJvDhK5YiTnHgpWhjLTAFuncwNCs2SwJw7KPFrx9pkMgwlAgMBAAECggEARzcWLUXBP8Olv5UFtf3PZvHfGTDDTt4Vq+LQGvw3I1if+5NF8ytq9ybo/PE1Qz69PZjSr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AgB4uq6m3skgiE17pk4Yi1FDRfhcSVGo3PP88ifX9Ga9jtoeXlvWAE96M=";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk86JnB46aPUkXhg2uDqdYYktn4UKhvFenaCJXiu6ZD9NF0tuTufE6MQJ6uYM8v/PvnF9OXEORcEsKulXxOPxyMQsM8awBVS5Fuya63dsWk396wNNesMpdleQE3DaCYxKAtG0gw9JilNCOFmL8x1v4WaoFTUDV8XCPo5d/0ptb/r1TUtA+NEBUCLYFOMf4F9xtuXTLjp1VhPI0WVwHM6pmSfpMiPsMljphnxvqr1mlJFRqRIpriHs0zKZPMjQfsLh98O39yZmJ+Y+2JlbnlG/LjnNIdXBA7gtomiXwRn6KXmEKXtee4t6HLK24GdqcwE87xsR9Na8FHlKs6JBCQE3lQIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "http://122.112.156.218:8080/test/test";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        //config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";
        return config;
    }

    private static String genOutTradeNo() {
        String s = String.valueOf(new Random().nextInt(99999));
        String s2 = String.valueOf(new Date().getTime()) + s;
        return DigestUtils.md5DigestAsHex(s.getBytes(StandardCharsets.UTF_8));
    }
}
